import Vue from "vue";
import Vuex from "vuex";
import api from '../api/request.js'
Vue.use(Vuex);

const store = new Vuex.Store({
	state: {
		token: uni.getStorageSync('AppToken') || '',
		userInfo: uni.getStorageSync('userInfo') || {},
		tagList: [], //客户标签列表
		bankInfo: null, //结算账户信息
		version: '1.0.0', //版本号
	},
	mutations: {
		setToken(state, token) {
			state.token = token;
			uni.setStorageSync('AppToken', token)
		},
		async setUserInfo(state) {
			const userInfo = await api.get('/app/getUserInfo', {}, false)
			if (userInfo.code !== 200) return
			const cityPointInfo = await api.get('/app/pickuppoint/getInfo', {
				id: userInfo.login_user_key.user.pickupPointId
			}, false)
			const bankInfo = await api.get('/app/citicbank-user/getInfoByCdpId', {
				cdpId: cityPointInfo.data.id
			}, false)
			const supplyWarehouseInfo = await api.get('/app/supplywarehouse/getInfo', {
				id: '494841976588337152'
			}, false)
			const data = {
				...userInfo.login_user_key.user,
				cityPointInfo: cityPointInfo.data,
				supplyWarehouseInfo: supplyWarehouseInfo ? supplyWarehouseInfo.data : {},
				resource: userInfo.login_user_key.permissions
			}
			uni.setStorageSync('userInfo', data)
			state.bankInfo = bankInfo.data;
			state.userInfo = data;
			console.log('userInfo.data', data)
		},
		setBankInfo(state, bankInfo) {
			state.bankInfo = bankInfo;
		},
		async setTagList(state) {
			const data = await api.post('/app/pickuppoint/customer-label/listNoPage', {
				pickupPointId: state.userInfo.pickupPointId
			})
			if (data.code !== 200) return
			state.tagList = data.data;
		},
		setVersion(state, version) {
			state.version = version;
		},
		logout(state) {
			state.token = '';
			uni.setStorageSync('AppToken', '')
			state.userInfo = {};
			uni.setStorageSync('userInfo', '')
		}
	}
})

export default store